package com.ly.heart.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.TenderEntity;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.ICustomerService;
import com.ly.heart.service.ITenderService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.EnsureMapper;
import com.ly.heart.domain.EnsureEntity;
import com.ly.heart.service.IEnsureService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 交/退保证金记录Service业务层处理
 *
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class EnsureServiceImpl extends ServiceImpl<EnsureMapper,EnsureEntity> implements IEnsureService
{
    @Resource
    private EnsureMapper ensureMapper;
    @Resource
    private ICustomerService customerInfoService;
    @Resource
    private ITenderService tenderService;
    @Resource
    private IActModelService actModelService;

    /**
     * 查询交/退保证金记录
     *
     * @param id 交/退保证金记录主键
     * @return 交/退保证金记录
     */
    @Override
    public EnsureEntity selectEnsureEntityById(String id)
    {
        EnsureEntity ensureEntity = ensureMapper.selectEnsureEntityById(id);
        TenderEntity tenderEntity = tenderService.getById(ensureEntity.getTenderId());
        ensureEntity.setTender(Optional.ofNullable(tenderEntity).orElse(new TenderEntity()));
        return ensureEntity;
    }

    /**
     * 查询交/退保证金记录列表
     *
     * @param ensureEntity 交/退保证金记录
     * @return 交/退保证金记录
     */
    @Override
    public List<EnsureEntity> selectEnsureEntityList(EnsureEntity ensureEntity)
    {
        List<EnsureEntity> ensureList = ensureMapper.selectEnsureEntityList(ensureEntity);
        if(!CollectionUtils.isEmpty(ensureList)){
            ensureList.forEach(ensure ->  {
                TenderEntity tenderEntity = tenderService.getById(ensure.getTenderId());
                ensure.setTender(Optional.ofNullable(tenderEntity).orElse(new TenderEntity()));
            });
        }
        return ensureList;
    }

    /**
     * 新增交/退保证金记录
     *
     * @param ensureEntity 交/退保证金记录
     * @return 结果
     */
    @Override
    public int insertEnsureEntity(EnsureEntity ensureEntity)
    {
        String ensureId = SnowflakeIdWorker.getInstance().nextStringId();
        ensureEntity.setId(ensureId);
        ensureEntity.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        ensureEntity.setCreateTime(DateUtils.getNowDate());
        StartProcessDto startDto = new StartProcessDto().setBusinessId(ensureId).setBusinessType(ensureEntity.getBusinessType()).setModelId(ensureEntity.getModelId());
        startDto.setBusinessName("保证金_".concat(ensureId));
        String instanceId = actModelService.startProcess(startDto);
        ensureEntity.setInstanceId(instanceId);
        ensureEntity.setState(ConstUtil.STATE_IN_PROCESS);
        return ensureMapper.insertEnsureEntity(ensureEntity);
    }

    /**
     * 修改交/退保证金记录
     *
     * @param ensureEntity 交/退保证金记录
     * @return 结果
     */
    @Override
    public int updateEnsureEntity(EnsureEntity ensureEntity)
    {
        ensureEntity.setChangeTime(DateUtils.getNowDate());
        return ensureMapper.updateEnsureEntity(ensureEntity);
    }

    /**
     * 批量删除交/退保证金记录
     *
     * @param ids 需要删除的交/退保证金记录主键
     * @return 结果
     */
    @Override
    public int deleteEnsureEntityByIds(String[] ids)
    {
        return ensureMapper.deleteEnsureEntityByIds(ids);
    }

    /**
     * 删除交/退保证金记录信息
     *
     * @param id 交/退保证金记录主键
     * @return 结果
     */
    @Override
    public int deleteEnsureEntityById(String id)
    {
        return ensureMapper.deleteEnsureEntityById(id);
    }
}
